package cn.itcast.itcaststore.web.servlet.client;

import cn.itcast.itcaststore.domain.User;
import cn.itcast.itcaststore.exception.LoginException;
import cn.itcast.itcaststore.serive.UserService;
import cn.itcast.itcaststore.serive.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/*
 * 登录操作
 * @throws javax.servlet.ServletException, IOException
 *
 * */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取登录页面输入的用户名与密码
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        //2.调用service完成登录操作
        UserService service =new UserServiceImpl();
        try {
            User user=service.login(username,password);
            //3.登录成功，将用户存储到session中。
            request.getSession().setAttribute("user",user);
            //获取用户的角色，其中用户的角色分为普通用户和超级用户两种
            String role=user.getRole();
            //如果是超级用户，就进入到传智书城的后台管理系统；否则进入我的账户页面
            if ("超级用户".equals(role)){
                response.sendRedirect(request.getContextPath()+"/admin/login/home.jsp");
                return;
            }else {
                response.sendRedirect(request.getContextPath()+"/client/myAccount.jsp");
                return;
            }
        } catch (LoginException e) {
            //如果出现问题，将错误信息存储到request，并跳转回登录页面显示错误信息
            e.printStackTrace();
            request.setAttribute("register_message",e.getMessage());
            request.getRequestDispatcher("/client/login.jsp").forward(request,response);
        }
    }
}
